﻿using System;
using NPoco;

namespace OrchardFork.Framework.Data
{
    [AttributeUsage(AttributeTargets.Method)]
    public class TransactionAttribute : Attribute, ITransactionAttribute
    {
        private readonly IDatabase _database;
        public TransactionAttribute()
        {
        }

        //在生命周期内，获取数据连接池
        public TransactionAttribute(IBaseDao baseDao)
        {
            _database = baseDao.GetDatabase();
        }

        public virtual void OnBegin(bool isTransaction)
        {
            if (isTransaction)
                _database.BeginTransaction();
        }

        public virtual void OnComplete(bool isTransaction)
        {
            if (isTransaction)
                _database.CompleteTransaction();
        }

        public virtual void OnException(bool isTransaction)
        {
            if (isTransaction)
                _database.AbortTransaction();
        }
    }
}
